<?php
/**
 * @file: thread.php
 * @User: shiwuhao
 * @Date: 16/5/6 16:06
 * @synopsis: 科室model
 */

class Threadm extends CI_Model
{
    const TOP_ID_ROOT = 0;      // 父级分类
    const TOP_ID_JP = 1;        // 解剖分类
    const TOP_ID_ZY = 2;        // 专业分类
    const TOP_ID_HT = 500;      // 话题分类

    const THREAD_LIST_BY_TOP_ID = 'thread_list_by_top_id_';

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 根据科室ID获取科室名称
     * @param $thread_id
     * @return string
     */
    public function get_thread_name($thread_id)
    {
        $thread_name = '';
        switch($thread_id) {
            case -1:
                $thread_name = '医学生';
                break;
            case -2:
                $thread_name = '实习医生';
                break;
            default:
                $thread_list = $this->get_thread_list();
                $thread_name = $thread_list[$thread_id];
        }

        return $thread_name;
    }

    /**
     * 获取科室列表
     */
    public function get_thread_list()
    {
        $key = THREAD_LIST_PREFIX;
        $list = unserialize($this->redis->get($key));
        if (!$list) {
            $result = $this->db->select('id, thread_name')
                ->from('ml_thread')
                ->where(array('is_show' => 1))
                ->get()
                ->result_array();
            $list = array_column($result, 'thread_name', 'id');

            $this->redis->save($key, serialize($list), THREAD_LIST_KEY_EXPIRE_TIME);
        }

        return $list;
    }

    /**
     * 树形显示科室列表
     */
    public function get_tree_thread_list()
    {
        $key = THREAD_LIST_TREE_PREFIX;
        $list = unserialize($this->redis->get($key));
        if (!$list) {
            $list = array();
            $result = $this->db->select('id, thread_name, parent_id, sort, top_id')
                ->from('ml_thread')
                ->where(array('is_show' => 1))
                ->order_by('sort', 'ASC')
                ->get()
                ->result_array();

            $list = list_to_tree($result, 'id', 'parent_id', 'content', 0);

            if ($list) {
                $this->redis->save($key, serialize($list), THREAD_LIST_KEY_EXPIRE_TIME);
            }
        }

        return $list;
    }

    /**
     * 树形科室列表 显示用户订阅状态
     * @param $user_id
     */
    public function get_tree_rss_thread_list($user_id)
    {
        $this->load->model('rssm');
        $rss_thread = $this->rssm->get_rss_thread($user_id);
        $rss_topic = $this->rssm->get_rss_topic($user_id);
        $rss_thread = array_merge($rss_thread, $rss_topic);

        $thread_list = $this->get_tree_thread_list();

        $this->config->load('thread');
        $topic_id    = $this->config->item('topic_id');
        $thread_type = $this->config->item('thread_type');
        $topic_type  = $this->config->item('topic_type');

        foreach($thread_list as &$granddad) {
            foreach($granddad['content'] as &$parents) {
                if (isset($parents['content']) && $parents['content']) {
                    foreach($parents['content'] as &$child){
                        // 拼接订阅状态
                        if (in_array($child['id'], $rss_thread)) {
                            $child['rss_status'] = '1';
                        } else {
                            $child['rss_status'] = '0';
                        }

                        // 拼接 APP跳转识别 type 值
                        if ($child['top_id'] == $topic_id) {
                            $child['type'] = $topic_type;
                        } else {
                            $child['type'] = $thread_type;
                        }
                    }
                }
            }
        }

        return $thread_list;
    }


    /**
     * @param int $top_id: 0root分类,1解剖分类,2专业分类,500话题分类
     * @return array|mixed
     */
    public function get_thread_list_by_top_id($top_id)
    {
        $key = self::THREAD_LIST_BY_TOP_ID . $top_id;

        $data = $this->mc->get($key);
        if (empty($data)) {
            $data = $this->db->where(array('top_id' => $top_id, 'is_show' => 1))->get('ml_thread')->result_array();

            $this->mc->save($key, $data, 30 * 60);
        }

        return $data;
    }

    /**
     * 获取话题分类所有ID
     */
    public function get_topic_ids()
    {
        $key = TOPIC_ALL_IDS;

        $ids = unserialize($this->redis->get($key));
        if (empty($ids)) {
            $this->config->load('thread');
            $parent_id = $this->config->item('topic_id');
            $filter = array('parent_id >' => $parent_id, 'top_id' => $parent_id);

            $result = $this->db->where($filter)->get('ml_thread')->result_array();
            $ids = array_column($result, 'id');
            if (!empty($ids)) {
                $this->redis->save($key, serialize($ids), TOPIC_ALL_IDS_EXPIRE_TIME);
            }
        }

        return $ids;
    }

    /**
     * 获取单条分类信息
     * @param $thread_id
     * @return array
     */
    public function get_info($thread_id)
    {
        return $this->db->where('id', $thread_id)->limit(1)->get('ml_thread')->row_array();
    }

    /**
     * 获取单条标签信息
     * @param $tag_id
     * @return array
     */
    public function get_tag_info($tag_id)
    {
        return $this->db->where('id', $tag_id)->limit(1)->get('ml_tags')->row_array();
    }

    /**
     * 获取帖子所在科室ID
     * @param $post_id
     * @return array
     */
    public function get_thread_ids_by_post($post_id)
    {
        $filter = array('post_id' => $post_id, 'type' => 1);
        $result = $this->db->select('type_id')->from('ml_posttype')->where($filter)->get()->result_array();
        if (!$result) return array();

        return array_column($result, 'type_id');
    }
    /**
     * 获取所以的科室id
     * 内科 9;外科10;其他11
     */
    public function get_thread_all_ids()
    {
        $key   = THREAD_ALL_IDS;
        $ids = unserialize($this->redis->get($key));
        if(empty($ids))
        {
            $array = array(9,10,11);
            $res   = $this->db->select('id')->from('ml_thread')->where_in('parent_id',$array)->get()->result_array();
            $ids   =  array_column($res,'id');
            if (!empty($ids)) {
                $this->redis->save($key, serialize($ids),0);
            }
        }
        return $ids;

    }
}